﻿using System;
using System.Windows;
using EzDesk.DesktopClient.FunctionWindowModule.ViewContract;
using EzDesk.DesktopClient.FunctionWindowModule.ViewModel;
using EzDesk.Generic.WPF;
using EzDesk.DesktopClient.ViewBase;
using EzDesk.Generic;


namespace EzDesk.DesktopClient.FunctionWindowModule
{
    /// <summary>
    /// Interaction logic for FunctionPageView.xaml
    /// </summary>
    public partial class FunctionPageView : WindowBaseNotMainView, IFunctionPageView
    {
        private readonly WebKit.WebKitBrowser _webContent = new WebKit.WebKitBrowser(true);

        public dynamic Document
        {
            get
            {
                try
                {
                    return _webContent.Document;
                }
                catch
                {
                    return null;
                }
            }
        }


        public FunctionPageView()
        {
            InitializeComponent();
            this.Dispatcher.BeginInvoke(new Action(this.Init));

            SizeChanged += FunctionPageView_SizeChanged;
            this.Closed += (o, e) =>
                               {
                                   ViewModel.CloseCommand.Execute();
                                   this._webContent.Dispose();
                               };

        }

        void FunctionPageView_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // LogEx.TRACE("FunctionPageView", "width: " + ActualWidth + "  height: " + ActualHeight);
        }

        private void Init()
        {
            InitWebControl();
            InitProperty();
            InitWindowState();
        }

        private void InitWebControl()
        {
            WebHost.Child = _webContent;
            _webContent.DocumentTitleChanged += new EventHandler(_webContent_DocumentTitleChanged);
        }

        void _webContent_DocumentTitleChanged(object sender, EventArgs e)
        {
            Title = _webContent.DocumentTitle;
        }

        private void InitProperty()
        {
            YOffsetWithMainWindow = 0;
        }

        private void InitWindowState()
        {
            this.ShowInTaskbar = true;
        }

        void IFunctionPageView.Navigate(string url)
        {
            _webContent.Navigate(url);
            Title = "页面载入中...";
            this.ToBeTop();
        }

        protected override void OnMainWindowMove(Event.MainWindowMoveArgs args)
        {
        }

        public IFunctionPageViewModel ViewModel
        {
            get { return DataContext as IFunctionPageViewModel; }
        }
    }
}
